package com.earthea.scaffold.domain;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.earthea.scaffold.common.constant.Constants;
import com.earthea.scaffold.common.entity.BaseUpdatableEntity;
import com.earthea.scaffold.common.enums.CommonStatusEnum;
import com.earthea.scaffold.common.enums.GenderEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@TableName("t_user")
public class User extends BaseUpdatableEntity {
    /**
     * 用户id
     */
    @TableId(type = IdType.AUTO)
    private Long userId;

    /**
     * 用户名
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String username;

    /**
     * 手机号
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String phone;

    /**
     * 邮箱
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String email;

    /**
     * 密码
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String password;

    /**
     * 用户性别（0男 1女 2未知）
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private GenderEnum sex;

    /**
     * 头像ID
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL, updateStrategy = FieldStrategy.NOT_EMPTY)
    private Long avatarId;

    /**
     * 帐号状态（0正常 1停用）
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL, updateStrategy = FieldStrategy.NOT_NULL)
    private CommonStatusEnum status;

    /**
     * 最后登录IP
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL, updateStrategy = FieldStrategy.NOT_EMPTY)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @TableField(insertStrategy = FieldStrategy.NOT_NULL, updateStrategy = FieldStrategy.NOT_EMPTY)
    private String loginTime;

    /**
     * 包含admin角色
     */
    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    /**
     * userId为1是默认超级管理员
     */
    public static boolean isAdmin(Long userId) {
        return userId != null && userId == Constants.ADMIN_ID;
    }
}
